//
//  LoginViewController.m
//  OrderFood
//
//  Created by xiaofang wang on 2018/10/22.
//  Copyright © 2018年 zhiyuan. All rights reserved.
//

#import "LoginViewController.h"
//#import "ForgotPWViewController.h" //12-25不要了
#import "BaseTabBarViewController.h"


@interface LoginViewController ()
@property (nonatomic,strong) UITextField *accountTF;
@property (nonatomic,strong) UIButton *loginBtn;
@property (nonatomic,strong) UIButton *forgotPBtn;
@end

@implementation LoginViewController
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.name = @"登录";
    self.showBack = NO;
    self.view.backgroundColor = WHITECOLOR;
    [self setupUI];
}
-(void)setupUI{
    
    for (int i = 0; i < 2; i++) {
        [QTTools tool_addLineWithFrame:CGRectMake(15, 150+50*i, SCREEN_WIDTH - 30, 1) superView:self.view];
    }
    
    self.accountTF = [[UITextField alloc] initWithFrame:CGRectMake(15, 100, SCREEN_WIDTH-75, 50)];
    self.accountTF.placeholder = @"请输入手机号";
    [self.accountTF setValue:themeTextColor forKeyPath:@"_placeholderLabel.textColor"];
    [self.accountTF setValue:[UIFont systemFontOfSize:15] forKeyPath:@"_placeholderLabel.font"];
    self.accountTF.keyboardType = UIKeyboardTypeNumberPad;
    [self.view addSubview:self.accountTF];
    
    self.passwordTF = [[UITextField alloc] initWithFrame:CGRectMake(15, 100+50, SCREEN_WIDTH-75, 50)];
    self.passwordTF.placeholder = @"请输入密码";
    [self.passwordTF setValue:themeTextColor forKeyPath:@"_placeholderLabel.textColor"];
    [self.passwordTF setValue:[UIFont systemFontOfSize:15] forKeyPath:@"_placeholderLabel.font"];
    self.passwordTF.secureTextEntry = YES;
    [self.view addSubview:self.passwordTF];
    
    UIButton *seeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    seeBtn.frame = CGRectMake(GetMaxX(self.passwordTF), GetMinY(self.passwordTF)+8, 44, 34);
    [seeBtn setImage:IMAGE(@"by") forState:UIControlStateNormal];
    [self.view addSubview:seeBtn];
    seeBtn.selected = NO;
    [seeBtn addTarget:self action:@selector(seeAction:) forControlEvents:UIControlEventTouchUpInside];
    
    _loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _loginBtn.frame = CGRectMake(15, GetMaxY(self.passwordTF)+35, SCREEN_WIDTH-30, 44);
    _loginBtn.backgroundColor = themeColor;
    _loginBtn.layer.cornerRadius = 5;
    _loginBtn.layer.masksToBounds = YES;
    _loginBtn.titleLabel.font = [UIFont boldSystemFontOfSize:17];
    [_loginBtn setTitleColor:WHITECOLOR forState:UIControlStateNormal];
    [_loginBtn setTitle:@"登 录" forState:UIControlStateNormal];
    [_loginBtn addTarget:self action:@selector(login:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.loginBtn];
    
    _forgotPBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _forgotPBtn.frame = CGRectMake(SCREEN_WIDTH - 85, GetMaxY(_loginBtn)+17, 70, 40);
    [_forgotPBtn setTitle:@"忘记密码?" forState:UIControlStateNormal];
    _forgotPBtn.titleLabel.font = FONT(15);
    _forgotPBtn.titleLabel.textAlignment = NSTextAlignmentRight;
    [_forgotPBtn setTitleColor:themeTextColor forState:UIControlStateNormal];
    [_forgotPBtn addTarget:self action:@selector(forgotAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.forgotPBtn];
    
    
    //1.不能用了 默认密码 外网
//    self.passwordTF.text = @"wangxiaofang";
//    self.accountTF.text = @"wangxiaofang";
    
    //2.内网
    self.passwordTF.text = @"bobo";
    self.accountTF.text = @"bobo";

    //3.有数据测试账号
//    self.passwordTF.text = @"spring";
//    self.accountTF.text = @"1234";
    
    //12-29自动登录
    [self login:self.loginBtn];
}

-(void)forgotAction{
    [IndicatorManager showDimMessage:@"请联系门店管理人员重置密码"];
}


#pragma mark - 登录
-(void)login:(UIButton *)sender{
    
    if (_accountTF.text.length != 0 && _passwordTF.text.length != 0) {
        [HUD showOperateBegin:@""];
        [NetWork netWorkLogin:_accountTF.text andPwd:_passwordTF.text success:^(NSDictionary * _Nonnull response) {
            [HUD showOperateEnd];
            if(RES_SUCESS) {
                [USERDEFAULT setObject:[[response objectForKey:@"data"] objectForKey:@"token"] forKey:TOKEN_KEY];
                [USERDEFAULT setBool:YES forKey:LOGIN];
               
                //切换根视图控制器
                BaseTabBarViewController *baseTabBarVC = [[BaseTabBarViewController alloc] init];
                baseTabBarVC.selectedIndex = 0;
                [UIApplication sharedApplication].keyWindow.rootViewController = baseTabBarVC;
                
            }else{
                [HUD showMessage:@"您输入的账号或密码错误"];
            }
        } failure:^(NSString * _Nonnull message) {
            [HUD showOperateEnd];
            [HUD showMessage:FAIL_MESSAGE];
        }];
    }
    
    
    
}
-(void)seeAction:(UIButton *)sender{
    sender.selected = !sender.selected;
    if (sender.selected) {
        [sender setImage:IMAGE(@"zy") forState:UIControlStateNormal];
        self.passwordTF.secureTextEntry = NO;
    }else{
        [sender setImage:IMAGE(@"by") forState:UIControlStateNormal];
        self.passwordTF.secureTextEntry = YES;
        
    }
  
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
